﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace SoonCode.FocusTest.ViewModels
{
    internal class ScreenManager
    {
        private static ScreenManager _instance;
        private static readonly Object lockObject = new Object();

        public static ScreenManager Instance
        {
            get
            {
                lock (lockObject)
                {
                    if (_instance == null) _instance = new ScreenManager();
                    return _instance;
                }
            }
        }

        private List<Screen> screens;
        private ScreenManager()
        {
            screens = new List<Screen>();
            screens.Add(Screen.PrimaryScreen);
            foreach (var screen in Screen.AllScreens)
            {
                if (!screen.Primary) screens.Add(screen);
            }
        }

        public static int GetScreenCount()
        {
            return Screen.AllScreens.Length;
        }

        public void Show(Window owner, Window self, int screenIndex = 0, bool fullScreen = true)
        {
            Rectangle workingArea = this.screens[screenIndex].WorkingArea;
            self.Left = workingArea.Left;
            self.Top = workingArea.Top;
            self.Width = workingArea.Width;
            self.Height = workingArea.Height;
            if (fullScreen)
            {
                self.SourceInitialized += (e, r) =>
                {
                    self.WindowState = WindowState.Maximized;
                };
            }
            self.Owner=owner;
            self.Show();
        }

        public bool? ShowDialog(Window owner, Window self, int screenIndex = 0, bool fullScreen = true)
        {
            Rectangle workingArea = this.screens[screenIndex].WorkingArea;
            self.Left = workingArea.Left;
            self.Top = workingArea.Top;
            self.Width = workingArea.Width;
            self.Height = workingArea.Height;
            if (fullScreen)
            {
                self.SourceInitialized += (e, r) =>
                {
                    self.WindowState = WindowState.Maximized;
                };
            }
            self.Owner = owner;
            return self.ShowDialog();
        }
    }
}
